#include <main.h>
#include <DS1302.C>
#include <2416.C>
#int_RB
unsigned int gValtimer1;
BYTE gCount,gYear,gMth,gDay,gDow,gHou,gMin,gSec;
void  RB_isr(void) 
{

}

#int_EXT
void  EXT_isr(void) 
{
 gValtimer1=(get_timer1()*0.8)/60;
 set_timer1(0);
  if(gCount++>30){gCount=0;
   rtc_get_time(gHou,gMin,gSec);
   rtc_get_date(gDay,gMth,gYear,gDow);
  }
}



void main()
{
   int16 secDelay,minDelay,houDelay;
   int16 order[3]={0};
   
   
   

   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);      //52.4 ms overflow
   //setup_timer_2(T2_DIV_BY_1,0,1);      //.2 us overflow, .2 us interrupt
   rtc_init();
   init_ext_eeprom();
   enable_interrupts(INT_RB);
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);
   //delay 10s de on dinh mach
   delay_ms(10000);

    //Example blinking LED program
    while(true){
     computeDelayTime(& secDelay,& minDelay,& houDelay, gHou, gMin,gSec );
     sort_3(order,secDelay,minDelay,houDelay);
     delay_us(order[0]);
     if(order[0]== secDelay)
     //display second
     //display();
     if(order[0]== minDelay)
     //display minute
     //display();
     if(order[0]== houDelay)
     //display hour
     //display();
     
     
     delay_us(order[1]- order[0]);
     if(order[1]== secDelay)
     //display second
     //display();
     if(order[1]== minDelay)
     //display minute
     //display();
     if(order[1]== houDelay)
     //display hour
     //display();
     
     delay_us(order[2]- order[1]);
     if(order[2]== secDelay)
     //display second
     //display();
     if(order[2]== minDelay)
     //display minute
     //display();
     if(order[2]== houDelay)
     //display hour
     //display();
     delay_us(60*gValtimer1 - order[2]);
         
      
    }

}
void computeDelayTime(int16 *secDelay,int16 *minDelay,int16 *houDelay,BYTE gHou,BYTE gMin,BYTE gSec ){
      *secDelay=(int16) gSec*gValtimer1; 
      *minDelay=(int16) gMin*gValtimer1; 
      *houDelay=(int16) (5*gHou*gValtimer1 + (gMin*gValtimer1)/10);   
}

void swapint(int16 *a, int16 *b)
{
   int16 temp;
   
   temp = *a;
   *a = *b;
   *b = temp;
   }

void sort_3( int16 order[], int16 hour, int16 min, int16 sec)
{
   order[0]=hour;
   order[1]=min;
   order[2]=sec;
   
   if(order[0] > order[1])
   {
      swapint(&order[0],&order[1]);
   }
   if(order[1] > order[2])
   {
      swapint(&order[1],&order[2]);
   }
   if(order[0] > order[1])
   {
      swapint(&order[0],&order[1]);
   }
   }
void display_frame(BYTE cmd[]){
 
     output_high(PIN_A0);
     put_byte(cmd[0]);    
     output_low(PIN_A0);
     
     output_high(PIN_E0);
     put_byte(cmd[1]);    
     output_low(PIN_E0);
     
     output_high(PIN_E1);
     put_byte(cmd[2]);    
     output_low(PIN_E1);
     
     output_high(PIN_E2);
     put_byte(cmd[3]);    
     output_low(PIN_E2);
     
         
}
void put_byte(BYTE cmd){
   int8 i=0;
   // This shifts 8 bits out PIN_D0-PIN_D7 with add, LSB first.
   for(i=0;i<8;i++)
      output_bit(PIN_D0+i,shift_right(&cmd,1,0));
   //delay_us(gValtimer1/8);
   output_d (0x00);
}
